package com.bestcem.xm.deliver.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bestcem.xm.common.core.exception.BusinessException;
import com.bestcem.xm.component.mybatis.service.impl.XmServiceImpl;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.componet.redis.service.RedisService;
import com.bestcem.xm.deliver.entity.DeliverSmsBalance;
import com.bestcem.xm.deliver.mapper.DeliverSmsBalanceMapper;
import com.bestcem.xm.deliver.service.DeliverSmsBalanceService;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author been
 * @description 针对表【ds_deliver_smsbalance】的数据库操作Service实现
 * @createDate 2022-11-07 13:49:46
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DeliverSmsBalanceServiceImpl extends XmServiceImpl<DeliverSmsBalanceMapper, DeliverSmsBalance>
        implements DeliverSmsBalanceService {

    private final static String SMS_COMPONENT_CODE_KEY = "SMS_COMPONENT_CODE_KEY";
    private @NonNull RedisService redisService;
    @Value("${sms.smsType:push}")
    private String smsType;

    @Override
    public JSONObject getSmsBalance() {
        LambdaQueryWrapper<DeliverSmsBalance> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeliverSmsBalance::getOrgId, SecurityContextHolder.getOrgId());
        queryWrapper.last("limit 1");
        DeliverSmsBalance deliverSmsBalance = baseMapper.selectOne(queryWrapper);
        if (Objects.isNull(deliverSmsBalance)) {
            //插入
            deliverSmsBalance = new DeliverSmsBalance();
            deliverSmsBalance.setId(new ObjectId().toHexString());
            deliverSmsBalance.setOrgId(SecurityContextHolder.getOrgId());
            if (StrUtil.equals(smsType, "push")) {
                Object code = redisService.hashGet(SMS_COMPONENT_CODE_KEY, SecurityContextHolder.getOrgCode());
                if (Objects.isNull(code)) {
                    log.error("请检查sms-starter初始化是否成功注册到push");
                    deliverSmsBalance.setCode("10010");
                } else {
                    deliverSmsBalance.setCode((String) code);
                }
            } else {
                log.error("发送短信不使用push平台");
                deliverSmsBalance.setCode("10086");
            }

            deliverSmsBalance.setBalance(1000);
            deliverSmsBalance.setSignature("【倍市得CEM】");
            //为单位更新后的版本
            deliverSmsBalance.setVersion(2);
            baseMapper.insert(deliverSmsBalance);
        }
        JSONObject data = new JSONObject();
        data.put("balance", deliverSmsBalance.getBalance());
        data.put("balance_count", deliverSmsBalance.getBalance());
        data.put("success_number", 0);
        data.put("id", deliverSmsBalance.getId());
        data.put("signature", deliverSmsBalance.getSignature());
        JSONObject ret = new JSONObject();
        ret.put("data", data);
        return ret;
    }

    @Override
    public JSONObject updateSignature(String id, JSONObject param) {
        baseMapper.update(
                null,
                Wrappers.<DeliverSmsBalance>lambdaUpdate()
                        .set(DeliverSmsBalance::getUpdatedDt, LocalDateTime.now())
                        .set(DeliverSmsBalance::getSignature, "【" + param.getString("signature") + "】")
                        .eq(DeliverSmsBalance::getId, id)
        );
        JSONObject data = new JSONObject();
        data.put("id", id);
        JSONObject ret = new JSONObject();
        ret.put("data", data);
        return ret;
    }
}




